package com.myrule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;

/**
 * @author: 21050134
 * @date: 2023/9/15 14:26
 * @description: 定制负载均衡策略
 */
public class MyRandomRule extends AbstractLoadBalancerRule {

	private int total = 0;            // 总共被调用的次数，目前要求每台被调用5次
	private int currentIndex = 0;    // 当前提供服务的机器号



	private Server choose(ILoadBalancer lb, Object key) {
		if (lb == null) {
			return null;
		}
		Server server = null;
		while (server == null) {
			if (Thread.interrupted()) {
				return null;
			}
			//获取所有有效的服务实例列表
			List<Server> upList = lb.getReachableServers();
			//获取所有的服务实例的列表
			List<Server> allList = lb.getAllServers();
			//如果没有任何的服务实例则返回 null
			int serverCount = allList.size();
			if (serverCount == 0) {
				return null;
			}
			//与随机策略相似，但每个服务实例只有在调用 3 次之后，才会调用其他的服务实例
			if (total < 3) {
				server = upList.get(currentIndex);
				total++;
			} else {
				total = 0;
				currentIndex++;
				if (currentIndex >= upList.size()) {
					currentIndex = 0;
				}
			}
			if (server == null) {
				Thread.yield();
				continue;
			}
			if (server.isAlive()) {
				return (server);
			}
			server = null;
			Thread.yield();
		}
		return server;
	}



	@Override
	public Server choose(Object key) {
		return choose(getLoadBalancer(), key);
	}



	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig) {
		// TODO Auto-generated method stub
	}

}
